package com.and.action;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.wltea.analyzer.lucene.IKAnalyzer;

import com.and.analyzer.Analyzers;
import com.and.bean.Participle;
import com.and.bean.ParticipleCategory;
import com.and.dao.Dao;
import com.and.dao.QueryResult;

@Controller
public class ParticipleAction {

	@Resource
	protected Dao dao;

	private Integer id;
	private String speech;
	private long amount;
	private ParticipleCategory participleCategory;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSpeech() {
		return speech;
	}

	public void setSpeech(String speech) {
		this.speech = speech;
	}

	public long getAmount() {
		return amount;
	}

	public void setAmount(long amount) {
		this.amount = amount;
	}

	public ParticipleCategory getParticipleCategory() {
		return participleCategory;
	}

	public void setParticipleCategory(ParticipleCategory participleCategory) {
		this.participleCategory = participleCategory;
	}

	public String jumpPass() {
		System.out.println("------------:" + id);
		return "jumpPass";
	}

	public String save() {

		System.out.println("------------:" + speech);
		try {
			List<String> listAnalyzer = Analyzers.testAnalyzer(
					new IKAnalyzer(), speech);
			for (String string : listAnalyzer) {

				QueryResult<Participle> query = dao.getScrollData(
						Participle.class, "speech = ?1",
						new Object[] { string.trim() });
				if (query.getGetall().size() == 0) {
					Participle mParticiple = new Participle();
					mParticiple.setSpeech(string.trim());
					mParticiple.setParticipleCategory(dao.find(
							ParticipleCategory.class, id));
					dao.save(mParticiple);
					System.out.println("-----" + string);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "save";
	}
	
	public String findAllPartiiple(){
		 LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		 map.put("amount", "DESC");//ASC
		ParticipleCategory mParticipleCategory = dao.find(ParticipleCategory.class, id);
		QueryResult<Participle> query = dao.getScrollData(
				Participle.class, "participleCategory = ?1",
				new Object[] { mParticipleCategory },map);
		List<Participle>list = query.getGetall();
		String string = "";
		for (Participle participle : list) {
			if(string.equals("")){
				string =participle.getSpeech()+"("+participle.getAmount()+")";
			}else{
				string = string+","+participle.getSpeech()+"("+participle.getAmount()+")";
			}
		}
		System.out.println(string);
		
		ServletActionContext.getRequest().setAttribute("category", mParticipleCategory.getCategory());
		ServletActionContext.getRequest().setAttribute("string", string);
		return"findAllPartiiple";
	}
	
	
	//listfindCategory.jsp
	private QueryResult<ParticipleCategory> cust;
	public String findSpeech(){
		int pid =0;
		System.out.println("-----------////--111-----:"+pid);	
		QueryResult<Participle> query = dao.getScrollData(
				Participle.class, "speech = ?1",
				new Object[] { speech.trim() });
		System.out.println("----------query.getGetall().size()--111-----:"+query.getGetall().size());	
		if(query.getGetall().size()==1){
			List<Participle>list = query.getGetall();
			for (Participle participle : list) {
				pid=participle.getId();
			}
		}
		if(pid!=0){
			System.out.println("-----------////-------:"+pid);
			ServletActionContext.getRequest().setAttribute("pid", pid);
			ServletActionContext.getRequest().setAttribute("speech", speech);
			
			cust= dao.getScrollData(ParticipleCategory.class);
			ServletActionContext.getRequest().setAttribute("orgs", cust);
		}
	
		
		return "findSpeech";
	}

}
